package com.yongbao.common.scheduler;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.yongbao.common.Constants;
import com.yongbao.common.domain.BaseEntity;
import com.yongbao.common.service.RedisService;
import com.yongbao.model.domain.DishOrderDO;
import com.yongbao.service.DishOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class ScheduledService {

    @Autowired
    private DishOrderService dishOrderService;
    @Autowired
    private RedisService redisService;

    @Scheduled(cron = "0 0/5 * * * ?")
    public void scheduled() {
        log.info("=====>>>>>使用cron  {}", System.currentTimeMillis());
        // 查询所有的商家地址信息
        DateTime yesterday = DateUtil.offsetDay(new Date(), -1);
        DateTime startTime = DateUtil.beginOfDay(yesterday);
        DateTime endTime = DateUtil.endOfDay(yesterday);
        List<DishOrderDO> dishOrderDOList = dishOrderService.lambdaQuery().lt(BaseEntity::getCreateTime, endTime).ge(BaseEntity::getCreateTime, startTime).list();
        for (DishOrderDO dishOrderDO : dishOrderDOList) {
            redisService.clearGeo(Constants.MAIN_ORDER, String.valueOf(dishOrderDO.getId()));
        }
    }
}
